{% extends "base.html" %}

{% block extrajs %}
<script type="text/javascript">
$(document).ready( function() {
    var changere = new RegExp("\\dh");
    if ((new Date()).getDay() < 2) {
      // sun/mon check past 2 days
      changere = new RegExp("\\b(([1-2]\\.\\dd)|\\dh)");
    }
    $(".assignee").each(function() {
        if ($(this).text().match(changere)) {
          $(this).parent().css('font-weight', 'bold');
          $(this).parent().parent().css('background-color', '#ffa');
        }
      });
  });

{% if authenticated %}
    var drag_and_drop_handler = function(event,ui) {
    $('td.cards_backlog').sortable('disable');
    var sorted_cards = $('td.cards_backlog').sortable('serialize', {
        expression: /(.+)[_](.+)/
    });
    $.ajax({
        type: "POST",
        data: sorted_cards,
        url: "{{ url_for('team_backlog', team_slug=team.slug) }}",
        error: function(jqXHR, textStatus, errorThrown) {
            window.alert(textStatus + ": " + errorThrown);
        },
        success: function(data, textStatus, jqXHR) {
            $('td.cards_backlog').sortable('enable');
            var response = $.parseJSON(data);
            if (window.console) {
                console.log(textStatus + ": " + data['message']);
            }
        }
    });
}

$(document).ready(function() {
    $('td.cards_backlog').on('sortupdate', drag_and_drop_handler);
    $('td.cards_backlog').sortable('enable');
});
{% endif %}
</script>
{% endblock extrajs %}

{% block content %}


{% if metrics %}
<table class="metrics"><tr>
<td class="metric metrics_key">
    <table>
    <tr><th>KPI</th></tr>
    <tr><th>Last 4 weeks</th></tr>
    <tr><th>&Delta;</th></tr>
    </table>
</td>
{% for m in metrics %}
<td class="metric" id="metric_{{ m['name']|slugify }}">
    <table>
    <tr><th>{{ m['name'] }}</th></tr>
    {% if m['name'] == "Hit SLA" %}
        <tr><td>{{ "%d%%"|format(round(m['value'] * 100)) }}</td></tr>
        <tr><td class="delta_{% if m['delta'] >= 0 %}positive{% else %}negative{% endif %}">{% if m['delta'] >= 0 %}+{% endif %}{{ "%d%%"|format(round(m['delta'] * 100)) }}</td></tr>
    {% else %}
        <tr><td>{{ m['value'] }}</td></tr>
        <tr><td class="delta_{% if m['delta'] > 0 %}positive{% else %}negative{% endif %}">{% if m['delta'] >= 0 %}+{% endif %}{{ m['delta'] }}</td></tr>
    {% endif %}
    </table>
</td>
{% endfor %}
</tr></table>
{% endif %}

<p class="nav" id="team_report_nav"><strong>How's {{ team.name }} doing?</strong>:
{% for report in report_config %}
    <a href="/reports/{{ team.slug }}/{{ report['slug'] }}/{% if report['months'] %}{{ report['months'] }}/{% endif %}">{{ report['name'] }}</a>{% if not loop.last %} / {% endif %}
{% endfor %}
</p>

<p class="nav">
{% for team in teams %}
    <a href="{{ url_for('team', team_slug=team.slug) }}">{{ team.name }}</a>{% if not loop.last %} / {% endif %}
{% endfor %}
</p>



<div class="{% if team.slug %}team_page_board{% else %}overview_board{% endif %}" id="content_detail">

<table class="board_enhanced board">
    <tr>
        {% for column in board.columns %}
        <th class="col_wip_{{ column.wip_state }}">
            <div>
                {{ column.name.replace("Backlog", "Ready: Elabo").replace("OTIS", "TIE") }}
                {{ column.wip }}
                {% if column.wip_limit %}/ {{ column.wip_limit }}{% endif %}
            </div>
        </th>
        {% endfor %}
    </tr>

    <tr>
        {% for column in board.columns %}
        <td class="cards_{{ column.name|slugify }}">
            {% for card in column.cards %}

            {% if backlog_markers %}
                {%- if loop.index0 % weekly_throughput == 0 %}
                    <div class="split_cell_header ui-state-default ui-state-disabled">
                        Roughly: Week of {{ backlog_markers.pop(0).strftime("%m/%d") }}
                    </div>
                {% endif %}
            {% endif %}


            {{ card_detail(card, request) }}
            {% endfor %}

            {% for placeholder in column.placeholders %}
            <div class="card_on_board card_placeholder">
                <!-- Placeholder #{{ placeholder }} -->
                &nbsp;<br />
                &nbsp;<br />
                &nbsp;
            </div>
            {% endfor %}

            {% if column.buffer %}
                <div class="split_cell_header">
                    {{ column.buffer.replace("Backlog", "Ready: Elabo").replace("OTIS", "TIE") }}
                </div>
                {% for card in column.buffer_cards %}
                {{ card_detail(card, request) }}
                {% endfor %}
            {% endif %}

            {% if column.name == board.states.backlog %}
                <h3><a href="{{ url_for('team_backlog', team_slug=team.slug) }}">Full list &raquo;</a></h3>
            {% endif %}

        </td>
        {% endfor %}
    </tr>
</table>

</div>


{% endblock content %}


{% import "card_macros.html" as card_macros %}

{% macro card_detail(card, request) -%}
    {% set vs_goal = card.cycle_vs_goal %}
    <div class="card_on_board {% if vs_goal == 2 %}cycle_double_over_goal{% elif vs_goal == 1 %}cycle_over_goal{% elif vs_goal == 0 %}cycle_in_goal{% endif %} card_type_{{ card.type|slugify }} {% if card.blocked %}card_blocked{% endif %}" id="card_{{ card.key }}">
        <div class="card_key"><span>{{ card.key }}</span>
            {% if card.done_date %}
                - {{ card.cycle_time }}d / {{ card.cycle_goal.upper }}d
            {% elif card.start_date %}
                - {{ card.current_cycle_time() }}d / {{ card.cycle_goal.upper }}d
            {% elif card.priority %}
                - #{{ card.priority }}
            {% endif %}
            {% if not card.is_card %}<i class="icon-plus-sign-alt" title="{{ card.type }}"></i>{% endif %}
            {% if 'Expedite' in card.service_class['name'] %}<i class="icon-forward" title="{{ card.service_class['name'] }}"></i>
            {% elif 'Date' in card.service_class['name'] %}<i class="icon-calendar" title="{{ card.service_class['name'] }}"></i>{% endif %}
        </div>

        <div class="card_contents">
        <div class="title">
            <p class="title"><a href="{{ url_for('card', key=card.key) }}">{{ card.title }}</a></p>

            {% if card.blocked %}
            <div class="blocker">
                <p>{{ card_macros.hours_to_days(card.blockers[-1].duration) }} - {{ card.blockers[-1].reason }}</p>
            </div>
        {% endif %}

        </div>


        <p class="assignee">
            {{ card_macros.hours_to_days(card.time_in_state) }} <br />
            {% if card.due_date %}Due: {{ card.due_date.strftime("%m/%d") }}{% endif %}
            {% if card.service_class['name'] in ['Intangible', 'Not Prioritized'] %}
                {{ card.service_class.get('name') }}
                {% if card.worked_on %} &ndash; {% endif %}
            {% endif %}

            {% if card.state not in ["Backlog", "Done"] %}
                {% if card.worked_on %}
                    {% for worker in card.worked_on %}
                        <a href="{{ url_for('person', name=worker) }}">{{ worker }}</a>{% if not loop.last %},{% endif %}
                    {% endfor %}
                {% endif %}
            {% endif %}
        </p>
        </div>

    </div>
{%- endmacro %}
